package com.yinhe.module.xjy.calendar;

/**
 * @author: admin
 * @date: 2022/7/27 17:02
 * @description:
 */

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhCalendar;
import com.yinhe.system.service.IYhCalendarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 日程Controller
 *
 * @author yinhe
 * @date 2022-07-27
 */
@RestController
@RequestMapping("/xjy/calendar")
public class XjyCalendarController extends BaseController
{
    @Autowired
    private IYhCalendarService yhCalendarService;

    /**
     * 查询日程列表
     */
    /*@PreAuthorize("@ss.hasPermi('system:calendar:list')")*/
    @GetMapping("/list")
    public TableDataInfo list(YhCalendar yhCalendar)
    {
        startPage();
        List<YhCalendar> list = yhCalendarService.selectYhCalendarList(yhCalendar);

        return getDataTable(list);
    }

    /**
     * 查询共享日程列表
     */
  /*  @PreAuthorize("@ss.hasPermi('system:calendar:share')")*/
    @GetMapping("/share")
    public TableDataInfo share(YhCalendar yhCalendar)
    {
        startPage();
        List<YhCalendar> list = yhCalendarService.shareYhCalendarList(yhCalendar);
        //System.out.println("1111"+list);
        return getDataTable(list);
    }


    /**
     * 导出日程列表
     */
    @PreAuthorize("@ss.hasPermi('system:calendar:export')")
    @Log(title = "日程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhCalendar yhCalendar)
    {
        List<YhCalendar> list = yhCalendarService.selectYhCalendarList(yhCalendar);
        ExcelUtil<YhCalendar> util = new ExcelUtil<YhCalendar>(YhCalendar.class);
        util.exportExcel(response, list, "日程数据");
    }

    /**
     * 获取日程详细信息
     */
    /*@PreAuthorize("@ss.hasPermi('system:calendar:query')")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhCalendarService.selectYhCalendarById(id));
    }

    /**
     * 新增日程
     */
    /*@PreAuthorize("@ss.hasPermi('system:calendar:add')")*/
    @Log(title = "日程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhCalendar yhCalendar)
    {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhCalendar.setUserId(sysUser.getUserId());
        yhCalendar.setDeptId(sysUser.getDeptId());
        return toAjax(yhCalendarService.insertYhCalendar(yhCalendar));
    }

    /**
     *@author admin
     *@Description  主页显示日程
     *@Date 14:00 2022/7/29
     *@Param
     *@Return
     **/
    @RequestMapping("/search/{date}")
    public  AjaxResult calendar(@PathVariable String date) throws ParseException {
       Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(date);

       List<YhCalendar>  result =  yhCalendarService.searchEvents(date1);
        //System.out.println("输出列表数据的多少"+result.size());
        return AjaxResult.success(result);
    }

    /**
     * 修改日程
     */
    /*@PreAuthorize("@ss.hasPermi('system:calendar:edit')")*/
    @Log(title = "日程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhCalendar yhCalendar)
    {

        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhCalendar.setUserId(sysUser.getUserId());
        yhCalendar.setDeptId(sysUser.getDeptId());
        return toAjax(yhCalendarService.updateYhCalendar(yhCalendar));
    }

    /**
     * 删除日程
     */
    /*@PreAuthorize("@ss.hasPermi('system:calendar:remove')")*/
    @Log(title = "日程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhCalendarService.deleteYhCalendarByIds(ids));
    }
}
